package rhc.singleNodeSpider.spider;

import rhc.singleNodeSpider.BasicComponent;
import rhc.singleNodeSpider.spider.component.SingleNodeSpiderCrawler;
import rhc.singleNodeSpider.spider.component.SingleNodeSpiderEngine;
import rhc.singleNodeSpider.spider.component.SingleNodeSpiderSchedule;
import rhc.singleNodeSpider.spider.impl.SingleNodeSpiderImpl;
import rhc.spider.crawler.Crawler;
import rhc.spider.engine.Engine;
import rhc.spider.schedule.Schedule;
import rhc.spider.setting.Setting;
import rhc.spider.spider.Spider;

/**spider 建造器
 * @author lenovo
 *
 */
public class SpiderBuilder extends BasicComponent {

	/**由SpiderSetting调用
	 * @param spiderSetting
	 * @return
	 */
	private static SingleNodeSpider build(SpiderSetting spiderSetting){
		String taskId=spiderSetting.getTaskId();
		Setting setting=spiderSetting.getSetting();
		Crawler crawler=spiderSetting.getCrawler();
		Spider spider=spiderSetting.getSpider();
		Engine engine=spiderSetting.getEngine();
		Schedule schedule=spiderSetting.getSchedule();
		
		crawler=(Crawler) (null==crawler?new SingleNodeSpiderCrawler(setting):crawler);
		engine=null==engine?new SingleNodeSpiderEngine(crawler,setting):engine;
		schedule=null==schedule?new SingleNodeSpiderSchedule(crawler,setting,taskId):schedule;
		//spider
		
		crawler.setEngine(engine);
		crawler.setSchedule(schedule);
		
		SingleNodeSpider singleNodeSpider=new SingleNodeSpiderImpl(crawler);
		
		return singleNodeSpider;
	}
	
	/**创建一个爬虫配置 
	 * @param taskId
	 * @return
	 */
	public static SpiderSetting create(String taskId){
		return new SpiderSetting(taskId);
	}
	
	/**配置爬虫的设置
	 * @author rhc
	 *
	 */
	public static class SpiderSetting{
		
		/**
		 * 任务id
		 */
		private String taskId;
		
		/**
		 * 配置
		 */
		private Setting setting;
		
		/**
		 * 爬虫处理器
		 */
		private Spider spider;
		
		private Engine engine;
		
		private Schedule schedule;
		
		private Crawler crawler;
		
		public SpiderSetting(String taskId){
			this.taskId=taskId;
		}

		public String getTaskId() {
			return taskId;
		}

		public void setTaskId(String taskId) {
			this.taskId = taskId;
		}

		public Setting getSetting() {
			return setting;
		}

		public void setSetting(Setting setting) {
			this.setting = setting;
		}

		public Spider getSpider() {
			return spider;
		}

		public void setSpider(Spider spider) {
			this.spider = spider;
		}

		public Engine getEngine() {
			return engine;
		}

		public void setEngine(Engine engine) {
			this.engine = engine;
		}

		public Schedule getSchedule() {
			return schedule;
		}

		public void setSchedule(Schedule schedule) {
			this.schedule = schedule;
		}
		
		public Crawler getCrawler() {
			return crawler;
		}

		public void setCrawler(Crawler crawler) {
			this.crawler = crawler;
		}

		public SingleNodeSpider build(){
			return SpiderBuilder.build(this);
		}
		
	}
	
}
